package com.gohome.gohomechat.ui.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Pair;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMMessage;
import com.gohome.gohomechat.GoHomeHelper;
import com.gohome.gohomechat.R;
import com.gohome.gohomechat.model.entify.ChatType;
import com.gohome.gohomechat.ui.activity.ChatActivity;
import com.gohome.gohomechat.ui.adapter.SessionListViewAdapter;
import com.gohome.gohomechat.utils.ToActivityUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.List;

/**
 * Created by dllo on 16/3/1.
 */
public class SessionFragment extends BaseFragment implements AdapterView.OnItemClickListener {
    private ListView session_listview;
    private SessionListViewAdapter adapter;
    private List<EMConversation> datas;
    public static final String NEWMESSAGENOTICE="新的消息到来";

    @Override
    protected int getLayout() {
        return R.layout.fragment_session;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initView(View view) {
        session_listview = (ListView) view.findViewById(R.id.session_listview);
        adapter = new SessionListViewAdapter();

        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(getActivity());
            IntentFilter intentFilter = new IntentFilter(NEWMESSAGENOTICE);
            localBroadcastManager.registerReceiver(broadcastReceiver, intentFilter);


    }

    @Override
    protected void initData() {
        session_listview.setAdapter(adapter);
        session_listview.setOnItemClickListener(this);

        datas=GoHomeHelper.getInstance().loadConversationList();
        adapter.addData(datas);
    }



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("content", new ChatType(EMMessage.ChatType.Chat, datas.get(position).getLastMessage().getUserName()));
        ToActivityUtil.startToActivity(getActivity(), ChatActivity.class, bundle);
        GoHomeHelper.getInstance().readMessage(datas.get(position).getLastMessage().getUserName());
    }

    private BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            datas=GoHomeHelper.getInstance().loadConversationList();
            adapter.addData(datas);
        }
    };

    @Override
    public void onResume() {
        datas=GoHomeHelper.getInstance().loadConversationList();
        adapter.addData(datas);
        super.onResume();
    }
}
